<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\facade\Session;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;

use app\admin\validate\OrderValidate;

use app\admin\model\{Order, OrderMake, Goods, Users, OrderEvaluate};

class OrderController extends BaseController
{
    public function order_list()
    {
        return view();
    }

    public function make_list()
    {
        return view();
    }

    public function order_evaluate_list()
    {
        return view();
    }

    // 获取订单数据列表
    public function order_data()
    {   
        $orderModel = new Order();
        $data = input('get.');

        // 验证数据
        try{
            validate(OrderValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where[] = ['del_time', '=', 0];

        // 搜索条件
        if($data && isset($data['key'])){
            if(!empty($data['key']['phone'])){
                $where[] = ['phone', 'like', '%' . $data['key']['phone'] . '%'];
            }

            if(!empty($data['key']['id'])){
                $where[] = ['id', '=', $data['key']['id']];
            }
        }

        $count = $orderModel->getNum($where)['data'];

        $list = $orderModel->getOrderList($where, $data['page'], $data['limit']);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    // 获取预约数据列表
    public function make_data()
    {   
        $orderModel = new OrderMake();
        $data = input('get.');

        // 验证数据
        try{
            validate(OrderValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where[] = ['del_time', '=', 0];

        // 搜索条件
        if($data && isset($data['key'])){
            if(!empty($data['key']['user_name'])){
                $user_where[] = ['nickname', 'like', '%' . $data['key']['user_name'] . '%'];
                $user_ids = (new Users())->where($user_where)->column('id');
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $count = $orderModel->getNum($where)['data'];
        
        $list = $orderModel->getMakeList($where, $data['page'], $data['limit']);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    //  预约删除
    public function make_del()
    {
        $orderModel = new Order();
        $data = input('post.');

        $flag = $orderModel->edit(['id' => $data['id'], 'del_time' => time()]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }

    // 获取订单评价数据列表
    public function order_evaluate_data()
    {   
        $orderModel = new Order();
        $goodsModel = new Goods();
        $userModel  = new Users();
        $orderEvaluateModel = new OrderEvaluate();
        $data = input('get.');

        $count = $orderEvaluateModel->getNum()['data'];

        $list = $orderEvaluateModel->getList(['del_time' => 0], 1, $data['page'], $data['limit'], '*', 'create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $orderInfo = $orderModel->getFind(['id' => $v['order_id']]);

            if($orderInfo['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            $goods_info = $goodsModel->getFind(['id' => $orderInfo['data']['goods_id']])['data'];

            $list['data'][$k]['goods_img']  = $goods_info['small_img'];
            $list['data'][$k]['goods_name'] = $goods_info['title'];
            $list['data'][$k]['user_name']  = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    //  订单评价删除
    public function order_evaluate_del()
    {
        $orderEvaluateModel = new OrderEvaluate();
        $data = input('post.');

        $flag = $orderEvaluateModel->edit(['id' => $data['id'], 'del_time' => time()]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
